package com.yourname.myapplication.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yourname.myapplication.App
import com.yourname.myapplication.data.model.Question
import com.yourname.myapplication.data.room.database.AppDatabase
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class RandomPracticeViewModel : ViewModel() {
    private val questionDao = AppDatabase.getInstance(App.instance).questionDao()
    var questions: List<Question> = emptyList()
    var currentQuestion: Question? = null

    init {
        viewModelScope.launch {
            questionDao.getAllQuestions().collect { allQuestions ->
                questions = allQuestions.shuffled()
                currentQuestion = questions.firstOrNull()
            }
        }
    }

    fun getNextRandomQuestion() {
        if (questions.isNotEmpty()) {
            questions = questions.drop(1)
            currentQuestion = questions.firstOrNull()
        }
    }
}